<?php

namespace app\model\hotel;

use app\model\BaseModel;

class Baoyu extends BaseModel
{

    protected $appId = '512';

    protected $appSecret = '10a7cdd970fe135cf4f7bb55c0e3b59f';

    public function header()
    {
        $time = time();

        $token = $this->appSecret.$time;

        $headers = array(
            'appId:'.$this->appId,
            'time:'.$time,
            'token:'.md5($token),
            'Accept: application/json',
            'Content-Type: application/json;',
            'User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
        );
        return $headers;
    }

    //房号列表
    public function roomList()
    {
        $url = "https://www.bypms.cn/open/room/list";

        $res = get($url, [],$this->header());

        $res = json_decode($res,true);

        return $this->success($res);
    }

    //房型读取
    public function roomTypeList()
    {
        $url = "https://www.bypms.cn/open/roomtype/list";

        $res = get($url, [],$this->header());

        $res = json_decode($res,true);

        return $this->success($res);
    }

    //是否可预定-单个房间
    public function canSchedule($roomId,$checkIn,$checkOut)
    {
        $url = "https://www.bypms.cn/open/room/bookable";

        $data = [
            'roomId'=>$roomId,
            'checkIn'=>$checkIn,
            'checkOut'=>$checkOut
        ];

        $res = get($url, $data,$this->header());

        $res = json_decode($res,true);

        return $this->success($res);
    }


    //是否可预定-多个房间
    public function canScheduleBatch($roomIds,$checkIn,$checkOut)
    {
        $url = "https://www.bypms.cn/open/room/bookable_batch";

        $data = [
            'roomIds'=>$roomIds,
            'checkIn'=>$checkIn,
            'checkOut'=>$checkOut
        ];

        $res = get($url, $data,$this->header());

        $res = json_decode($res,true);

        return $this->success($res);
    }


    //创建订单
    public function createOrder($data)
    {
        $url = "https://www.bypms.cn/open/contract/create";

        /*$params = [
            'channel'=>'',//订单显示来源的自定义名称
            'channelOrderId'=>'',//外部订单id
            'roomId'=>'',
            'checkIn'=>'',//入住时间
            'checkOut'=>'',//退房时间
            'renterName'=>'',//租客姓名
            'renterPhone'=>'',//租客手机
            'renterRemark'=>'',//备注
            'price'=>'',//订单金额，单位元
            'unitName'=>'',//订单对应的民宿侧房源名称
            'channelUnitId'=>'',//订单对应的民宿侧房源id，此项仅供民宿侧自用
        ];*/
        $data = json_encode($data,320);
        //dump($data);die;
        $res = post($url,$data,$this->header());

        $res = json_decode($res,true);

        return $this->success($res);
    }

    //取消订单
    public function cancelOrder($id)
    {
        $url = "https://www.bypms.cn/open/contract/cancel";

        $data = [
            'id'=>$id
        ];

        $res = get($url, $data,$this->header());

        $res = json_decode($res,true);

        return $this->success($res);
    }

}